[20220812_142139_user] D2_main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. * @attention
  7. *
  8. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  9. * All rights reserved.</center></h2>
  10. *
  11. * This software component is licensed by ST under BSD 3-Clause license,
  12. * the License; You may not use this file except in compliance with the
  13. * License. You may obtain a copy of the License at:
  14. * www.st.com/SLA0044
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "adc.h"
  22. #include "usart.h"
  23. #include "i2c.h"
  24. #include "tim.h"
  25. #include "stm32f4xx_hal.h"
  26. #include "gpio.h"
  27. #include "stdio.h"
  28. /* Private variables ---------------------------------------------------------*/
  29. /* Private variables ---------------------------------------------------------*/
  30. #define VECT_TAB_OFFSET 0x10000
  31. int tankstatus = 0;
  32. uint8_t zigbeecmd1[5]={0xFE,0x00,0x01,0x00,0x01};
  33. uint8_t zigbeecmd2[5]={0xFE,0x00,0x02,0x00,0x02};
  34. uint8_t zigbeestatu[20];
  35. uint8_t zigbeestatu1[5];
  36. uint8_t zigbeecon[7]="connect";
  37. uint8_t zigbeenotcon[11]="not connect";
  38. uint8_t zigbeechang[14]="change is down";
  39. uint8_t cmd[7]={0xFF,0x00,0x00,0x00,0x00,0x00,0x0D};
  40. HAL_StatusTypeDef Status;
  41. #define ADDR_AT24C04_WRITE_FIRST_16_PAGES 0xEC
  42. #define ADDR_AT24C04_WRITE_FIRST_16_PAGES 0xEC
  43. #define ADDR_AT24C04_READ 0xED
  44. #define AT24C04_TIMEOUT 0xED
  45. #define AT24C04_PAGE_SIZE 16
  46. #define BUFFER_SIZE 1
  47. uint16_t dig_T1;
  48. uint16_t dig_T2;
  49. uint16_t dig_T3;
  50. uint16_t dig_P1;
  51. uint16_t dig_P2;
  52. uint16_t dig_P3;
  53. uint16_t dig_P4;
  54. uint16_t dig_P5;
  55. uint16_t dig_P6;
  56. uint16_t dig_P7;
  57. uint16_t dig_P8;
  58. uint16_t dig_P9;
  59. uint32_t adc_P=0;
  60. uint32_t adc_T=0;
  61. uint8_t WriteBuffer[BUFFER_SIZE]={0xb6};
  62. uint8_t WriteBuffer1[BUFFER_SIZE]={0xff};
  63. uint8_t WriteBuffer2[BUFFER_SIZE]={0x00};
  64. uint8_t ReadBuffer[BUFFER_SIZE];
  65. int RH;
  66. float M15_BMP280 =0;
  67. /* USER CODE BEGIN PV */
  68. typedef void (*pFunction)(void);
  69. /* USER CODE END PV */
  70. /* Private function prototypes -----------------------------------------------*/
  71. void SystemClock_Config(void);
  72. void MX_ADC1_Init1(char pin);
  73. void sensor(void);
  74. HAL_StatusTypeDef AT24C04_Write(I2C_HandleTypeDef *hi2c,uint16_t MemAddress,uint8_t *pData);
  75. HAL_StatusTypeDef AT24C04_Read(I2C_HandleTypeDef *hi2c,uint16_t MemAddress,uint8_t *pData);
  76. long bmp280_T_MultipleReadThree(void);
  77. long bmp280_P_MultipleReadThree(void);
  78. short bmp280_MultipleReadTwo(uint16_t addr);
  79. /* USER CODE BEGIN PFP */
  80. /* USER CODE END PFP */
  81. /* Private user code ---------------------------------------------------------*/
  82. /* USER CODE BEGIN 0 */
  83. int _write(int fd, char *ch, int len)
  84. {
  85. HAL_UART_Transmit(&huart4, (uint8_t*)ch, len, 0xFFFF);
  86. HAL_UART_Transmit(&huart2, (uint8_t*)ch, len, 0xFFFF);
  87. return len;
  88. }
  89. void print(char* s, float c)
  90. { int w;
  91. int w_d;
  92. int w_f;
  93. if(c<0)
  94. {
  95. w = c*100;
  96. w_d = w/100;
  97. w_f = (-1*w)%100;
  98. }
  99. else
  100. {
  101. w = c*100;
  102. w_d = w/100;
  103. w_f = w%100;
  104. }
  105. printf("%s = %d.%d\n",s,w_d,w_f);
  106. }
  107. /* USER CODE END 0 */
  108. /**
  109. * @brief The application entry point.
  110. * @retval int
  111. */
  112. int main(void)
  113. {
  114. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET;
  115. HAL_Init();
  116. SystemClock_Config();
  117. MX_GPIO_Init();
  118. MX_USART2_UART_Init();
  119. MX_UART4_Init();
  120. MX_USART1_UART_Init();
  121. HAL_UART_Transmit(&huart2, (uint8_t *)zigbeecmd2, sizeof(zigbeecmd2), 10);
  122. HAL_Delay(1000);
  123. HAL_UART_Transmit(&huart2, (uint8_t *)zigbeecmd1, sizeof(zigbeecmd1), 10);
  124. HAL_UART_Receive(&huart2, (uint8_t *)zigbeestatu,sizeof(zigbeestatu),2000);
  125. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeestatu, sizeof(zigbeestatu),10);
  126. if (zigbeestatu[18]== 0x01 || zigbeestatu[18]== 0x02)
  127. {
  128. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeecon, sizeof(zigbeecon),10);
  129. }
  130. else if(zigbeestatu[18]== 0x05)
  131. {
  132. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeenotcon, sizeof(zigbeenotcon),10);
  133. }
  134. AT24C04_Write(&hi2c1,0xe0,WriteBuffer);
  135. AT24C04_Write(&hi2c1,0xf4,WriteBuffer1);
  136. AT24C04_Write(&hi2c1,0xf5,WriteBuffer2);
  137. dig_T1 = bmp280_MultipleReadTwo(0x88);
  138. dig_T2 = bmp280_MultipleReadTwo(0x8A);
  139. dig_T3 = bmp280_MultipleReadTwo(0x8C);
  140. dig_P1 = bmp280_MultipleReadTwo(0x8E);
  141. dig_P2 = bmp280_MultipleReadTwo(0x90);
  142. dig_P3 = bmp280_MultipleReadTwo(0x92);
  143. dig_P4 = bmp280_MultipleReadTwo(0x94);
  144. dig_P5 = bmp280_MultipleReadTwo(0x96);
  145. dig_P6 = bmp280_MultipleReadTwo(0x98);
  146. dig_P7 = bmp280_MultipleReadTwo(0x9A);
  147. dig_P8 = bmp280_MultipleReadTwo(0x9C);
  148. dig_P9 = bmp280_MultipleReadTwo(0x9E);
  149. while (1)
  150. {
  151. sensor();
  152. if(M15_BMP280<16)
  153. {
  154. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_10, GPIO_PIN_RESET);
  155. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8, GPIO_PIN_RESET);
  156. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_15, GPIO_PIN_RESET);
  157. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_13, GPIO_PIN_RESET);
  158. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_11, GPIO_PIN_RESET);
  159. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15, GPIO_PIN_RESET);
  160. HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15, GPIO_PIN_RESET);
  161. HAL_GPIO_WritePin(GPIOE,GPIO_PIN_13, GPIO_PIN_RESET);
  162. HAL_GPIO_WritePin(GPIOE,GPIO_PIN_11, GPIO_PIN_RESET);
  163. }
  164. }
  165. }
  166. void SystemClock_Config(void)
  167. {
  168. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  169. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  170. __HAL_RCC_PWR_CLK_ENABLE();
  171. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  172. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  173. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  174. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  175. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  176. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  177. RCC_OscInitStruct.PLL.PLLM = 8;
  178. RCC_OscInitStruct.PLL.PLLN = 72;
  179. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  180. RCC_OscInitStruct.PLL.PLLQ = 3;
  181. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  182. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  183. {
  184. Error_Handler();
  185. }
  186. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  187. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  188. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  189. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  190. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  191. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  192. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  193. {
  194. Error_Handler();
  195. }
  196. }
  197. void user_pwm_setvalue(uint16_t value)
  198. {
  199. TIM_OC_InitTypeDef sConfigOC;
  200. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  201. sConfigOC.Pulse = value;
  202. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  203. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  204. HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1);
  205. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
  206. }
  207. void user_pwm_setvalue2(uint16_t value)
  208. {
  209. TIM_OC_InitTypeDef sConfigOC;
  210. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  211. sConfigOC.Pulse = value;
  212. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  213. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  214. HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2);
  215. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
  216. }
  217. void sensor(void)
  218. {
  219. adc_T=bmp280_T_MultipleReadThree();
  220. adc_P=bmp280_P_MultipleReadThree();
  221. double var1, var2,t_fine;
  222. var1 = (((double) adc_T) / 16384.0 - ((double) dig_T1) / 1024.0)* ((double) dig_T2);
  223. var2 = ((((double) adc_T) / 131072.0 - ((double) dig_T1) / 8192.0)* (((double) adc_T) / 131072.0 - ((double) dig_T1) / 8192.0))* ((double) dig_T3);
  224. t_fine = (int32_t) (var1 + var2);
  225. double temperature = (var1 + var2) / 5120.0;
  226. double pressure;
  227. var1 = (t_fine / 2.0) - 64000.0;
  228. var2 = var1 * var1 * ((double) dig_P6) / 32768.0;
  229. var2 = var2 + var1 * ((double) dig_P5) * 2.0;
  230. var2 = (var2 / 4.0) + (((double) dig_P4) * 65536.0);
  231. var1 = (((double) dig_P3) * var1 * var1 / 524288.0+ ((double) dig_P2) * var1) / 524288.0;
  232. var1 = (1.0 + var1 / 32768.0) * ((double) dig_P1);
  233. if (var1 == 0.0)
  234. {
  235. var1 = 0;
  236. }
  237. pressure = 1048576.0 - (double) adc_P;
  238. pressure = (pressure - (var2 / 4096.0)) * 6250.0 / var1;
  239. var1 = ((double) dig_P9) * pressure * pressure / 2147483648.0;
  240. var2 = pressure * ((double) dig_P8) / 32768.0;
  241. pressure = pressure + (var1 + var2 + ((double) dig_P7)) / 16.0;
  242. cmd[1]=pressure/10000;
  243. int pa =pressure;
  244. cmd[2]=(pa%10000)/100;
  245. cmd[3]=(pa%10000)%100;
  246. HAL_UART_Transmit(&huart4, (uint8_t *)cmd, sizeof(cmd),1);
  247. HAL_Delay(10000);
  248. }
  249. HAL_StatusTypeDef AT24C04_Write(I2C_HandleTypeDef *hi2c,uint16_t MemAddress,uint8_t *pData)
  250. {
  251. Status = HAL_I2C_Mem_Write(&hi2c1, ADDR_AT24C04_WRITE_FIRST_16_PAGES, MemAddress, I2C_MEMADD_SIZE_8BIT, pData, AT24C04_PAGE_SIZE, AT24C04_TIMEOUT);
  252. return Status;
  253. }
  254. HAL_StatusTypeDef AT24C04_Read(I2C_HandleTypeDef *hi2c,uint16_t MemAddress,uint8_t *pData)
  255. {
  256. Status = HAL_I2C_Mem_Read(&hi2c1, ADDR_AT24C04_READ, MemAddress, I2C_MEMADD_SIZE_8BIT, pData, BUFFER_SIZE, AT24C04_TIMEOUT);
  257. return Status;
  258. }
  259. long bmp280_T_MultipleReadThree()
  260. {
  261. uint8_t msb, lsb, xlsb;
  262. int32_t temp = 0;
  263. AT24C04_Read(&hi2c1,0xfa,ReadBuffer);
  264. msb=ReadBuffer[0];
  265. AT24C04_Read(&hi2c1,0xfb,ReadBuffer);
  266. lsb=ReadBuffer[0];
  267. AT24C04_Read(&hi2c1,0xfc,ReadBuffer);
  268. xlsb=ReadBuffer[0];
  269. temp = ((msb << 12)|(lsb << 4)|(xlsb >> 4));
  270. return temp;
  271. }
  272. long bmp280_P_MultipleReadThree()
  273. {
  274. uint8_t msb, lsb, xlsb;
  275. int32_t pressure = 0;
  276. AT24C04_Read(&hi2c1,0xf7,ReadBuffer);
  277. msb=ReadBuffer[0];
  278. AT24C04_Read(&hi2c1,0xf8,ReadBuffer);
  279. lsb=ReadBuffer[0];
  280. AT24C04_Read(&hi2c1,0xf9,ReadBuffer);
  281. xlsb=ReadBuffer[0];
  282. pressure = ((msb << 12)|(lsb << 4)|(xlsb >> 4));
  283. return pressure;
  284. }
  285. short bmp280_MultipleReadTwo(uint16_t addr )
  286. {
  287. uint8_t msb, lsb;
  288. uint16_t temp = 0;
  289. AT24C04_Read(&hi2c1,addr,ReadBuffer);
  290. lsb = ReadBuffer[0];
  291. AT24C04_Read(&hi2c1,addr+1,ReadBuffer);
  292. msb = ReadBuffer[0];
  293. temp = msb << 8|lsb;
  294. return temp;
  295. }
  296. void MX_ADC1_Init1(char pin)
  297. {
  298. ADC_ChannelConfTypeDef sConfig = {0};
  299. hadc1.Instance = ADC1;
  300. hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
  301. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  302. hadc1.Init.ScanConvMode = DISABLE;
  303. hadc1.Init.ContinuousConvMode = DISABLE;
  304. hadc1.Init.DiscontinuousConvMode = DISABLE;
  305. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  306. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  307. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  308. hadc1.Init.NbrOfConversion = 1;
  309. hadc1.Init.DMAContinuousRequests = DISABLE;
  310. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  311. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  312. {
  313. Error_Handler();
  314. }
  315. sConfig.Channel = pin;
  316. sConfig.Rank = 1;
  317. sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  318. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  319. {
  320. Error_Handler();
  321. }
  322. }
  323. void MX_GPIO_Input1(unsigned long pin)
  324. {
  325. GPIO_InitTypeDef GPIO_InitStruct = {0};
  326. __HAL_RCC_GPIOE_CLK_ENABLE();
  327. GPIO_InitStruct.Pin = pin;
  328. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  329. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  330. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  331. }
  332. void Error_Handler(void)
  333. {
  334. }